package com.easycoding.part04;

import java.lang.reflect.Field;

/**
 * Created by hww on 2019/2/6 5:32 PM.
 */
public class ClassTest {

    private static int[] array = new int[3];

    private static int length = array.length;

    private static Class<One> one = One.class;

    private static Class<Another> another = Another.class;

    public static void main(String [] args) throws Exception {

        // 创建one的实例 并调用call方法
        One oneObject = one.newInstance();
        oneObject.call();

        // 创建another的实例 并调用speak方法
        Another anotherObject = another.newInstance();
        anotherObject.speak();

        // 打印修改前inner字段的值
        System.out.println(oneObject.getInner());

        // 通过反射获取inner属性对象
        Field privateFieldInOne = one.getDeclaredField("inner");

        // inner属性限制为private 设置为可修改
        privateFieldInOne.setAccessible(true);

        // 修改inner属性值
        privateFieldInOne.set(oneObject, "world change.");

        // 打印修改后inner字段的值
        System.out.println(oneObject.getInner());
    }
}

class One {

    private String inner = "time files.";

    public void call() {
        System.out.println("hello world.");
    }

    public String getInner() {
        return inner;
    }

}

class Another {

    public void speak() {
        System.out.println("easy coding");
    }

}